package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.CurtainContract;
import com.sunlg.smarthome.data.entity.Curtain;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.List;

/**
 * Created by sunlg on 2017/5/3.
 */

public class CurtainListPresenter implements CurtainContract.CurtainListPresenter {

    private CurtainContract.CurtainListView mView;
    private SHServiceWrapper mSHServiceWrapper;
    private PreferHelper mPreferHelper;

    public CurtainListPresenter() {
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
        mPreferHelper = new PreferHelper(App.getInstance(), "gateway");
    }

    @Override
    public void addCurtain(Curtain curtain) {

        curtain.setGatewayId(mPreferHelper.getInt("id", 0));

        mSHServiceWrapper.addCurtain(curtain, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if (data.getCode() == C.ActionStatus.SUCCESS){
                    loadAll();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void deleteCurtain(int curtainId) {
        mSHServiceWrapper.deleteCurtain(curtainId, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    loadAll();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void loadAll() {
        mSHServiceWrapper.getAllCurtain(mPreferHelper.getInt("id", 0),
                new MySubscriber<Result<List<Curtain>>>() {
            @Override
            public void onSuccess(Result<List<Curtain>> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.hideNoneCurtainText();
                    mView.showCurtainList(data.getData());
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void attachView(CurtainContract.CurtainListView view) {
        this.mView = view;
    }

    @Override
    public void dettachView() {
        this.mView = null;
    }
}
